package repository

import (
	"context"
	"demo/domain/model"
	"demo/infra/db"

	"gorm.io/gorm"
)

type userRepository struct {
	db *gorm.DB
}

func (u userRepository) Save(ctx context.Context, entity *model.User) error {
	rdb := db.GetDB(ctx, u.db)
	return rdb.WithContext(ctx).Create(entity).Error
}

func (u userRepository) FindByUserName(ctx context.Context, userName string) (*model.User, error) {
	var entity model.User
	err := u.db.WithContext(ctx).Find(&entity, "user_name=?", userName).Error
	return &entity, err
}

// NewUserRepository 创建一个新的 Repository 实例
func NewUserRepository(db *gorm.DB) model.UserRepository {
	return &userRepository{db: db}
}
